package com.itheima.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.CompanyDao;
import com.itheima.domain.store.Company;
import com.itheima.factory.MapperFactory;
import com.itheima.service.store.CompanyService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class CompanyServiceImpl implements CompanyService {

    @Override
    public void save(Company company) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            CompanyDao companyDao = MapperFactory.getMapper(sqlSession, CompanyDao.class);
            //使用UUID的生成策略来获取
            String id = UUID.randomUUID().toString();
            company.setId(id);
            //3.调用Dao层操作
            companyDao.save(company);
            //4.提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
        @Override
        public void delete (Company company){
            SqlSession sqlSession = null;
            try{
                //1.获取SqlSession
                sqlSession = MapperFactory.getSqlSession();
                //2.获取Dao
                CompanyDao companyDao = MapperFactory.getMapper(sqlSession,CompanyDao.class);
                //3.调用Dao层操作
                companyDao.delete(company);
                //4.提交事务
                TransactionUtil.commit(sqlSession);
            }catch (Exception e){
                TransactionUtil.rollback(sqlSession);
                throw new RuntimeException(e);
                //记录日志
            }finally {
                try {
                    TransactionUtil.close(sqlSession);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void update (Company company){
            SqlSession sqlSession = null;
            try{
                //1.获取SqlSession
                sqlSession = MapperFactory.getSqlSession();
                //2.获取Dao
                CompanyDao companyDao = MapperFactory.getMapper(sqlSession,CompanyDao.class);
                //3.调用Dao层操作
                companyDao.update(company);
                //4.提交事务
                TransactionUtil.commit(sqlSession);
            }catch (Exception e){
                TransactionUtil.rollback(sqlSession);
                throw new RuntimeException(e);
                //记录日志
            }finally {
                try {
                    TransactionUtil.close(sqlSession);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

        @Override
        public Company findById (String id){
            SqlSession sqlSession = null;
            try{
                //1.获取SqlSession
                sqlSession = MapperFactory.getSqlSession();
                //2.获取Dao
                CompanyDao companyDao = MapperFactory.getMapper(sqlSession,CompanyDao.class);
                //3.调用Dao层操作
                return companyDao.findById(id);
            }catch (Exception e){
                throw new RuntimeException(e);
                //记录日志
            }finally {
                try {
                    TransactionUtil.close(sqlSession);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

        @Override
        public List<Company> findAll () {
            SqlSession sqlSession = null;
            try{
                //1.获取SqlSession
                sqlSession = MapperFactory.getSqlSession();
                //2.获取Dao
                CompanyDao companyDao = MapperFactory.getMapper(sqlSession,CompanyDao.class);
                //3.调用Dao层操作
                return companyDao.findAll();
            }catch (Exception e){
                throw new RuntimeException(e);
                //记录日志
            }finally {
                try {
                    TransactionUtil.close(sqlSession);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

        @Override
        public PageInfo findAll ( int page, int size){
            SqlSession sqlSession = null;
            try{
                //1.获取SqlSession
                sqlSession = MapperFactory.getSqlSession();
                //2.获取Dao
                CompanyDao companyDao = MapperFactory.getMapper(sqlSession,CompanyDao.class);
                //3.调用Dao层操作
                PageHelper.startPage(page,size);
                List<Company> all = companyDao.findAll();
                PageInfo pageInfo = new PageInfo(all);
                return pageInfo;
            }catch (Exception e){
                throw new RuntimeException(e);
                //记录日志
            }finally {
                try {
                    TransactionUtil.close(sqlSession);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
